package com.xhd.base.session;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.exception.SessionException;
import com.xhd.exception.SystemException;
import com.xhd.server.Licence;
import com.xhd.server.ServerConfig;

public class SessionFileStore extends SessionStore {
	 private String jm84 = null;
	  private String jm85 = null;
	  private final String jm86 = ".s";
	  private static final Log jm9;
	  static Class jm10;
	 static
	  {
	    Class tmp3_0 = jm10;
	    if (tmp3_0 == null)
	    {
	   
	      try
	      {
	        SessionFileStore.jm10 = Class.forName("com.sinocc.base.session.SessionFileStore");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp3_0);
	  }
	 public SessionFileStore(String paramString1, String paramString2)
	  {
	    this.jm84 = paramString1;
	    this.jm85 = paramString2;
	  }

	  protected boolean load(Session paramSession)
	  {
	    if (paramSession == null)
	      return false;
	    boolean bool = false;
	    String str = this.jm85 + paramSession.getId() + ".s";
	    File localFile = new File(str);
	    if ((localFile != null) && (localFile.exists()) && (localFile.isFile()))
	    {
	      FileInputStream localFileInputStream = null;
	      try
	      {
	        localFileInputStream = new FileInputStream(localFile);
	        bool = load(paramSession);
	      }
	      catch (Exception localException1)
	      {
	        jm9.error(localException1);
	        try
	        {
	          if (localFileInputStream != null)
	            localFileInputStream.close();
	        }
	        catch (Exception localException3)
	        {
	        }
	      }
	      if ((!(bool)) && (this.jm84 != null))
	        try
	        {
	          localFile.delete();
	        }
	        catch (Exception localException2)
	        {
	        }
	    }
	    return bool;
	  }

	  protected boolean remove(String paramString)
	  {
	    if (paramString == null)
	      return false;
	    String str = this.jm85 + paramString + ".s";
	    File localFile = new File(str);
	    if (localFile.exists())
	      localFile.delete();
	    return false;
	  }

	  protected boolean save(Session paramSession)
	  {
	    if (paramSession == null)
	      return false;
	    try
	    {
	      String str = this.jm85 + paramSession.getId() + ".s";
	      File localFile = new File(str);
	      if ((localFile.exists()) && (!(localFile.delete())))
	        throw new SessionException(80011);
	      if (!(localFile.createNewFile()))
	        throw new SessionException(80012);
	      FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
	      boolean bool = save(paramSession);
	      localFileOutputStream.close();
	      return bool;
	    }
	    catch (Exception localException)
	    {
	      jm9.error(localException);
	    }
	    return false;
	  }

	  protected boolean save(Session paramSession, boolean paramBoolean)
	  {
	    return save(paramSession);
	  }

	  protected Hashtable loadAll()
	    throws SystemException
	  {
	    Hashtable localHashtable = new Hashtable();
	    File localFile1 = new File(this.jm85);
	    if (!(localFile1.exists()))
	    {
	      if (!(localFile1.mkdir()))
	        throw new SystemException(10029, this.jm85);
	      return localHashtable;
	    }
	    if (!(localFile1.isDirectory()))
	      throw new SystemException(10029, this.jm85);
	    File[] arrayOfFile = localFile1.listFiles();
	    if (arrayOfFile.length > 0)
	      for (int i = 0; i < arrayOfFile.length; ++i)
	      {
	        boolean bool = false;
	        Session localSession = null;
	        File localFile2 = arrayOfFile[i];
	        String str = localFile2.getName();
	        int j = str.length();
	        if ((j >= 38) && (str.substring(j - 2).equals(".s")))
	        {
	          if ((this.jm84 != null) && (str.startsWith(this.jm84 + ".")))
	            continue;
	          localSession = new Session();
	          FileInputStream localFileInputStream = null;
	          try
	          {
	            localFileInputStream = new FileInputStream(localFile2);
	            bool = load(localSession);
	          }
	          catch (Exception localException2)
	          {
	            if (ServerConfig.IsDebug)
	              jm9.error(localException2);
	          }
	          finally
	          {
	            try
	            {
	              if (localFileInputStream != null)
	                localFileInputStream.close();
	            }
	            catch (Exception localException3)
	            {
	            }
	          }
	        }
	        if ((!(bool)) || (!(localSession.isValid())))
	        {
	          if (this.jm84 == null)
	            continue;
	          try
	          {
	            localFile2.delete();
	          }
	          catch (Exception localException1)
	          {
	          }
	        }
	        else
	        {
	          Licence.setLicence(localSession.getGUID());
	          localHashtable.put(localSession.getId(), localSession);
	        }
	      }
	    return localHashtable;
	  }

	  protected boolean saveAttributes(Session paramSession, String paramString)
	  {
	    return false;
	  }

}
